package com.salt.core.sms.aliyun.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.salt.core.redis.cache.SaltRedis;
import com.salt.core.sms.aliyun.AliSmsTemplate;
import com.salt.core.sms.props.SmsProperties;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 阿里云短信配置类
 * @Author salt
 * @Date 2022/9/7
 * @Version 1.0.1
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnProperty(value = "salt.sms.name", havingValue = "aliyun")
public class AliSmsConfiguration {

    private static final String DOMAIN = "dysmsapi.aliyuncs.com";  //短信服务的服务接入地址
    private static final String PRODUCT = "Dysmsapi";

    private final SaltRedis saltRedis;

    @SneakyThrows
    @Bean
    public AliSmsTemplate aliSmsTemplate(SmsProperties smsProperties) {
        // 连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile(smsProperties.getRegionId(),
                smsProperties.getAccessKey(), smsProperties.getSecretKey());
        profile.addEndpoint(smsProperties.getEndpointName(), smsProperties.getRegionId(), PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        return new AliSmsTemplate(acsClient, saltRedis, smsProperties);
    }

}
